package com.kystar.muxplayer.base;

import android.util.Log;

import com.kystar.muxplayer.BuildConfig;


public class TLog {

    private static String LOG_TAG = "Kone";
    private static final boolean Debug = BuildConfig.DEBUG;

    public static final void v(Object... args) {
        println(Log.VERBOSE, args);
    }

    public static final void d(Object... args) {
        println(Log.DEBUG, args);
    }

    public static final void i(Object... args) {
        println(Log.INFO, args);
    }

    public static final void w(Object... args) {
        println(Log.WARN, args);
    }

    public static final void e(Object... args) {
        println(Log.ERROR, args);
    }

    private static String getLineNumber() {
        StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
        String caller = "<unknown>";
        for (int i = 3; i < trace.length; i++) {
            return trace[i].getClassName() + "  lineNumber:" + trace[i].getLineNumber() + " "
                + trace[i].getMethodName();
        }
        return "";
    }

    private static int println(int priority, Object... msgs) {
        if (!Debug) {
            return 0;
        }
        if (msgs.length == 0) {
            return Log.println(priority, LOG_TAG, getLineNumber());
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < msgs.length; i++) {
            if (msgs[i] instanceof byte[]) {
                sb.append(bytesToString((byte[]) msgs[i]));
            } else {
                sb.append(msgs[i]);
            }
            sb.append(" ");
        }
        return Log.println(priority, LOG_TAG, sb.toString());
    }

    public static String bytesToString(byte[] b) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            s.append(hex.toUpperCase());
            s.append(" ");
//            System.out.print(hex.toUpperCase() );
        }
        return s.toString();
    }
}
